/**
 *
 */
package com.hanhai.zrb.backend.admin.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hanhai.zrb.backend.admin.dao.mybatis.BackUserRoleDao;
import com.hanhai.zrb.backend.admin.dao.mybatis.PrivilegeDao;
import com.hanhai.zrb.backend.admin.model.Privilege;
import com.hanhai.zrb.backend.admin.service.BackUserService;
import com.hanhai.zrb.backend.admin.service.UserRoleService;
import com.hanhai.zrb.backend.util.MenuUtil;
import com.hanhai.zrb.backend.util.MergeUtil;

@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService {

	@Resource
	private BackUserService backUserService;
	@Resource
	private BackUserRoleDao backUserRoleDao;
	@Resource
	private PrivilegeDao privilegeDao;


	@Override
	public boolean add(Map<String, Object> user) {
		backUserService.addBackUser(user);
		
		user.put("user_id", user.get("id"));
		backUserRoleDao.add(user);
		return true;
	}

	@Override
	public boolean update(Map<String, Object> user) {
		backUserService.updateBackUser(user);
		user.put("user_id", user.get("id"));
		backUserRoleDao.update(user);
		return true;
	}

	@Override
	public List<Map<String, Object>> listAll() {
		return backUserService.listAll();
	}

	@Override
	public Map<String, Object> getDetail(Integer id) {
		Map<String, Object> user = backUserService.getBackUser(id);
		Map<String, Object> role = backUserRoleDao.get(id);
		MergeUtil.merge(user, role, "ur_");
		return user;
	}
	
	@Override
	public List<Privilege> findAllByUserId(Long userId) {
		List<Privilege> privilegeList = null;
		if (userId.equals(1L)) {
			privilegeList = privilegeDao.listAllMenu();
		} else {
			privilegeList = privilegeDao.listAllPrivilege(userId,true);
		}
		List<Privilege> rootList = MenuUtil.buildList(privilegeList);
		return rootList;
	}

}
